import { createStore } from 'vuex';
import apis from './rpcs';
import navs from './navs';

export default createStore({
  state: {
    navs,
    user: {
      userId: '',
      userAccount: '',
      userName: '',
      authenticated: false
    },
    navigateBack: {
    },
    navigateTitle: '',
    navigateRightRouter: {
    },
    navigateRightTitle: '',
    otherHeight: 0,
    area: {
      content: {
        height: 400
      },
      mainContentHeight: 500
    },
    workflowStatus: [
      { text: '流转中', value: 3, type: 'runing' },
      { text: '流转中(曾被拒绝过)', value: 4, type: 'runing' },
      { text: '流转结束', value: 0, type: 'fininshed' },
      { text: '用户取消', value: 1, type: 'finished' },
      { text: '管理员取消', value: 2, type: 'finished' }
    ],
    apis
  },
  mutations: {
    mainContentHeight (state, height) {
      state.area.mainContentHeight = height || 500;
    },
    logOn (state, user) {
      state.user.userId = user.userId || 'dc\\LuJuan1';
      state.user.userAccount = user.userAccount;
      state.user.userName = user.userName;
      state.user.authenticated = true;
    },

    logOff (state) {
      state.user.userId = null;
      state.user.userAccount = null;
      state.user.userName = null;

      state.user.authenticated = false;

      sessionStorage.clear();
    },
    fillConfigs (state, config) {
      state.user.userId = config.userId;
      state.user.userAccount = config.userAccount;
      state.user.userName = config.userName;

      state.user.authenticated = !!config.userId;

      sessionStorage.setItem('auth', JSON.stringify(state.user));
      sessionStorage.setItem('authedTime', new Date().getTime());
    },
    navigateBack (state, to) {
      let keys = Object.getOwnPropertyNames(to);

      state.navigateBack = {};

      keys.forEach(key => {
        state.navigateBack[key] = to[key];
      });
    },
    navigateTitle (state, title) {
      state.navigateTitle = title;
    },
    navigateRightTitle (state, title) {
      state.navigateRightTitle = title;
    },
    navigateRightRouter (state, to) {
      let keys = Object.getOwnPropertyNames(to);

      state.navigateRightRouter = {};

      keys.forEach(key => {
        state.navigateRightRouter[key] = to[key];
      });
    },
    otherHeight (state, height) {
      state.otherHeight = height;
    },
    contentArea (state, height, width) {
      state.area.content.height = height;
      state.area.content.width = width || 1024;
    }
  },
  getters: {
    currentUserId (state) {
      return () => {
        return state.user.userId || '';
      };
    },
    getApi (state) {
      return (key, needUserId) => {
        let url = state.apis[key];

        return needUserId ? `${url}?userId=${state.user.userId || ''}` : url;
      };
    }
  },
  actions: {
  },
  modules: {}
});